<?php

namespace Home\Controller;

class RecordsController extends HomeCoreController {
	
    public $Model = null;

    protected function _initialize(){
		
        parent::_initialize();
		
        $this->Model = D('Record');
		
    }
	
	public function index(){
		
		$id = I('get.id');
		
		if(IS_AJAX){
							
			$map['uid'] = $id;
			
			$map['mid'] = 1;
			
			$page_size = 10;
			
			$count = $this->Model->where($map)->count();
			
			$Page = new \Think\Page($count, $page_size);
			
			$list = $this->Model
				->where($map)
				->order("id desc")
				->limit($Page->firstRow . ',' . $Page->listRows)
				->select();
				
			$str = $this->tojson($list, $count);
			
			echo $str;
			
			exit;
			
        }else{
			
            $this->display();
			
        }
		
    }
	
	public function add(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$post_data['staff'] = json_encode($post_data['staff']);
			
			$data = $this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->update($data);
					
				if($result){
					
					echo json_encode(array('info'=>'操作成功', 'status'=>'y', 'id'=>$result));
												
				}else{
						
					$error = $this->Model->getError();
						
					echo json_encode(array('info'=>$error, 'status'=>'n'));
						
				}
					
			}else{
					
				$error = $this->Model->getError();
					
				echo json_encode(array('info'=>$error, 'status'=>'n'));
					
			}
												
		}else{
						
			$this->display();
			
		}
		
	}
	
	public function edit(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$map['id'] = $post_data['id'];
			
			/*$map['uid'] = $post_data['uid'];
			
			$map['mid'] = $post_data['mid'];
			
			$map['pid'] = $post_data['pid'];*/
			
			$info = $this->Model->where($map)->find();
			
			if(!empty($post_data['department'])){
			
				$department = explode(",", substr($post_data['department'],0,-1));
										
				$post_data['department'] = json_encode($department);
			
			}
			
			if(!empty($post_data['hint'])){
			
				if(!empty($info['hint'])){
						
					$post_data['hint'] = json_encode($post_data['hint']);
						
					$post_data['hint'] = $info['hint'] . ',' . $post_data['hint'];
						
				}else{
						
					$post_data['hint'] = json_encode($post_data['hint']);
						
				}
				
			}
			
			if(!empty($post_data['report'])){
				
				if(!empty($info['report'])){
					
					$post_data['report'] = json_encode($post_data['report']);
						
					$post_data['report'] = $info['report'] . ',' . $post_data['report'];
						
				}else{
						
					$post_data['report'] = json_encode($post_data['report']);
				
				}
				
			}
			
			$data = $this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->where($map)->save($data);
					
				if($result){

					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
												
				}else{
						
					$error = $this->Model->getError();
						
					echo json_encode(array('info'=>$error, 'status'=>'n'));
						
				}
					
			}else{
					
				$error = $this->Model->getError();
					
				echo json_encode(array('info'=>$error, 'status'=>'n'));
					
			}
												
		}else{
						
			$this->display();
			
		}
		
	}
	
	public function content(){
		
		$info = I('get.');
			
		$info = $this->Model->where(array('id'=>$info['id']))->find();
		
		$staff = json_decode($info['staff'], true);
		
		$this->assign('staff', $staff);	
		
		$this->assign('record', $info);	
		
		$scripts = M('Scripts')->find($info['sid']);
		
		$this->assign('scripts', $scripts);
			
		$department = json_decode($scripts['department']);
			
		$this->assign('department', $department);
		
		$map['sid'] = $info['sid'];
		
		$map['uid'] = $info['uid'];
		
		$map['mid'] = $info['mid'];
		
		$scene = M('Scene')->where($map)->order('tid asc')->select();
		
		foreach($scene as $k=>$v){
		
			$scene[$k]['department'] = json_decode($v['department']);
		
		}
		
		$this->assign('scene', $scene);
		
		$scene = M('Scene')->where($map)->order('tid asc')->select();
		
		$maps['sid'] = $info['sid'];
		
		$maps['uid'] = $info['uid'];
		
		$maps['mid'] = $info['mid'];
		
		$maps['rid'] = $info['id'];
		
		$hint = M('Hint')->where($maps)->select();
		
		foreach($hint as $k=>$v){
				
			$hint[$k]['voice'] = str_replace('./', '', $v['voice']);
				
		}
		
		$this->assign('hint', $hint);
		
		$report = M('Report')->where($maps)->select();
		
		foreach($report as $k=>$v){
				
			$report[$k]['voice'] = str_replace('./', '', $v['voice']);
				
		}
		
		$this->assign('report', $report);
		
        $this->display();
		
	}
	
	public function repair(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$data=$this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->where(array('id'=>$post_data['id']))->update($data);
				
				if($result){
					
					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
				}else{
					
					$error = $this->Model->getError();
					
					echo json_encode(array('info'=>$error, 'status'=>'n'));
					
				}
				
			}else{
				
                $error = $this->Model->getError();
				
                echo json_encode(array('info'=>$error, 'status'=>'n'));
				
			}
			
		}else{
			
			$info=I('get.');
			
			$info = $this->Model->where(array('id'=>$info['id']))->find();
			
			$this->assign('info', $info);
			
        	$this->display();
			
		}
		
	}
	
	public function del(){
		
		$ids = I('get.ids');
		
        $map['id'] = array('in', $ids);
		
        $res = $this->Model->where($map)->delete();
		
        if($res){
			
            echo json_encode(array('info'=>'删除成功', 'status'=>'y'));
			
        }else{
			
            $error = $this->Model->getError();
			
			echo json_encode(array('info'=>$error, 'status'=>'n'));
			
        }
		
	}
	
	public function build(){
		
		vendor("Tcpdf.tcpdf");
		
		$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
/*$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 061');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
*/
// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 061', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
	require_once(dirname(__FILE__).'/lang/eng.php');
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('stsongstdlight', '', 10);

// add a page
$pdf->AddPage();

$id = I('get.id');

$record = M('Record')->where(array('id'=>$id))->find();

$staff = json_decode($record['staff'], true);

$scripts = M('Scripts')->find($record['sid']);

$department = json_decode($scripts['department']);
			
$scene = M('Scene')->where(array('sid'=>$record['sid']))->order('tid asc')->select();

$hint = M('Hint')->where(array('rid'=>$record['id']))->select();

$report = M('Report')->where(array('rid'=>$record['id']))->select();

$html = '<div style="text-align:center"><h2>'.$record['title'] .'</h2><h2>演练记录</h2></div>' .

'<h1>基本信息</h1>'.

'<h3>演练名称</h3><div>'.$record['title'].'</div>'.

'<h3>演练主控官</h3><div>'.$record['management'].'</div>'.

'<h3>演练记录人</h3><div>'.$record['recordman'].'</div>'.

'<h3>演练时间</h3><div>'.date('Y-m-d H:i:s', $record['start_time']).' 至 ' .date('Y-m-d H:i:s', $record['end_time']).'</div>'.

'<h3>演练地点</h3><div>'.$record['address'].'</div>'.

'<h3>参演部门</h3>';

foreach($department as $k=>$v){
$html .= '<div>'.$v.'</div>';
}

$html .= '<h3>演练范围</h3><div>'.$scripts['range'].'</div>'.

'<h3>灾害介绍</h3><div>'.$scripts['brief'].'</div>'.

'<h3>演练目的</h3><div>'.$scripts['goal'].'</div>'.

'<h3>演练规则</h3><div>'.$scripts['rules'].'</div>'.

'<h1>演练内容</h1>';

foreach($scene as $ks=>$vs){

$ks = $ks + 1;

$vs['department'] = json_decode($vs['department']);

$html .= '<h3>场景'.$ks.'</h3><div>'.$vs['scenename'].'</div>'.

'<h3>场景描述</h3><div>'.$vs['brief'].'</div>'.

'<h3>部门响应</h3>';

foreach($vs['department'] as $a=>$b){
	
$a = $a + 1;

$html .= '<h3>'.$b.'</h3>';
	
foreach($hint as $c=>$d){
	
if(($vs['id'] == $d['hid']) && ($a == $d['did'])){

$html .= '<h3>演练时间</h3><div>'.date('Y-m-d H:i:s', $d['start_time']).' 至 ' .date('Y -m-d H:i:s', $d['end_time']).'
</div>'.'<div>'.$d['hint'].'</div>';	

}
	
}

}
	
}

$html .= '<h1>演练总结</h1>';

foreach($report as $e=>$f){
	
$html .= '<h3>'.$f['department'].'</h3><div>'.$f['hint'].'</div>';

}

$html .= '<h1>演练签到</h1>';

foreach($staff as $g=>$h){
	
$html .= '<h3>'.$h['department'].'</h3>';

foreach($h['staff'] as $i=>$j){
	
$html .= '<div>'.$j.'</div>';
	
}
	
}
		

$pdf->writeHTML($html, true, false, true, false, '');

$pdf->Output(date('YmdHis').'.pdf', 'I');

}
	
}